/*	Funciones
	@name = Obras
*/
//Asignar los Trabajador
function _asignWorker(Object){
	while (Object.tagName!='TR'){Object = Object.parentNode;}
	var id = Object.id;
	while (Object.id!='dataTable'){Object = Object.parentNode;}
	var name = Object.getAttribute('name');
	
	while (Object.id!='divBody'){Object = Object.parentNode;}
	var divData =  getElementById('divData',Object,"div");
	var url = _PATH+_SYSTEM+"?name="+name+"&type=showWorkers&ide="+id;
	divData.innerHTML="Cargando...";
	getPageHTML(url, divData);
}
//Agregar Trabajador
function _addWorker(Object){
	while (Object.id!='divData'){Object = Object.parentNode;}
	var tableSave = Object.getElementsByTagName("table");
	if (tableSave.length>=1)	tableSave = tableSave[0];
	var divData =  Object;
	var inputs = Object.getElementsByTagName("input");
	if (inputs.length>1) {	
		Obra = inputs[0];
		inputs = inputs[1];
	}
	while (Object.id!='divBody'){Object = Object.parentNode;}
	var table =   getElementById('dataTable',Object, "table");
	var page = table.getAttribute("page");
	var name = table.getAttribute("name");
	
	var url = _PATH+_SYSTEM+"?name="+name+"&type=saveWorker&ide="+Obra.value+"&id="+inputs.value;
	divData.innerHTML="Cargando...";
	//alert (url);
	getPageHTML(url, divData);
}
//Eliminar Trabajador
function _removeWorker(Object){
	while (Object.tagName!='TR'){Object = Object.parentNode;}
	var id = Object.id;
	while (Object.id!='divData'){Object = Object.parentNode;}
	var divData =  Object;
	var inputs = Object.getElementsByTagName("input");
	if (inputs.length>1) {	
		Obra = inputs[0];
	}
	while (Object.id!='divBody'){Object = Object.parentNode;}
	var table =   getElementById('dataTable',Object, "table");
	var page = table.getAttribute("page");
	var name = table.getAttribute("name");
	var url = _PATH+_SYSTEM+"?name="+name+"&type=removeWorker&ide="+Obra.value+"&id="+id;
	divData.innerHTML="Cargando...";
	getPageHTML(url, divData);	
}
//Mostrar Presupuestos
function _showBudget(Object){	
	while (Object.tagName!='TR'){Object = Object.parentNode;}
	var ide = Object.id;
	var div = openTabs("Presupuestos");
	var name= "cHByZXN1cHVlc3RvLnBocA==";
	var url = _PATH+_SYSTEM+"?name="+name+"&type=show&ide="+ide;
	getPageHTML(url, div);
}
//Culminar Obra
function _finishObra(Object){
	//Obtener Variables
	while (Object.tagName!='TR'){Object = Object.parentNode;}
	var id = Object.id;
	while (Object.id!='dataTable'){Object = Object.parentNode;}
	var name = Object.getAttribute('name');
	
	while (Object.id!='divBody'){Object = Object.parentNode;}
	var where = _get_Find(Object);
	var table =   getElementById('dataTable',Object, "table");
	var page = table.getAttribute("page");
	var name = table.getAttribute("name");
	
	var divData =  getElementById('divShow',Object,"div");
	var url = _PATH+_SYSTEM+"?name="+name+"&type=finishObra&ide="+id+"&where="+where+"&page="+page;
	divData.innerHTML="Cargando...";
	getPageHTML(url, divData);
}
/*
	@name = Presupuestos Materiales
*/
//
function _showTemplate(Object){
	while (Object.tagName!='TR'){Object = Object.parentNode;}
	var id = Object.id;
	while (Object.id!='dataTable'){Object = Object.parentNode;}
	var name = Object.getAttribute('name');
	
	while (Object.id!='divBody'){Object = Object.parentNode;}
	var divData =  getElementById('divData',Object,"div");
	var url = _PATH+_SYSTEM+"?name="+name+"&type=showTemplate&ide="+id;
	divData.innerHTML="Cargando...";
	getPageHTML(url, divData);
}

function _removeMaterial(Object){
	while (Object.tagName!='TR'){Object = Object.parentNode;}
	var id = Object.id;
	while (Object.id!='divData'){Object = Object.parentNode;}
	var inputs = Object.getElementsByTagName("input");
	if (inputs.length>1) {ide = inputs[0];}
	
	while (Object.id!='divBody'){Object = Object.parentNode;}
	var table =   getElementById('dataTable',Object, "table");
	var page = table.getAttribute("page");
	var name = table.getAttribute("name");
	
	//while (Object.id!='divBody'){Object = Object.parentNode;}
	//var divData =  getElementById('divData',Object,"div");
	
	var divMaterials =   getElementById('divMaterialsDetails',Object, "div");
	var url = _PATH+_SYSTEM+"?name="+name+"&type=removeMaterial&ide="+ide.value+"&id="+id;
	//alert (url);
	divMaterials.innerHTML="Cargando...";
	getPageHTML(url, divMaterials);
}

/*
	@name = Presupuestosss
*/
function _showMaterials(Object){
	while (Object.tagName!='TR'){Object = Object.parentNode;}
	var id = Object.id;
	while (Object.id!='dataTable'){Object = Object.parentNode;}
	var name = Object.getAttribute('name');
	
	while (Object.id!='divBody'){Object = Object.parentNode;}
	var divData =  getElementById('divData',Object,"div");
	var url = _PATH+_SYSTEM+"?name="+name+"&type=showMaterials&ide="+id;
	divData.innerHTML="Cargando...";
	getPageHTML(url, divData);
}

function _addMaterial(Object){
	while (Object.id!='divData'){Object = Object.parentNode;}
	var divData =  Object;
	var inputs = Object.getElementsByTagName("input");
	if (inputs.length>1) {	
		ide = 	inputs[0];
		can = inputs[1];
		mate = inputs[2];
	}
	var divMaterials =   getElementById('divMaterialsDetails',Object, "div");
	
	while (Object.id!='divBody'){Object = Object.parentNode;}
	var table =   getElementById('dataTable',Object, "table");
	var page = table.getAttribute("page");
	var name = table.getAttribute("name");
	
	var url = _PATH+_SYSTEM+"?name="+name+"&type=saveMaterial&ide="+ide.value+"&id="+mate.value+"&can="+can.value;
	divMaterials.innerHTML="Cargando...";
	//alert (url);
	getPageHTML(url, divMaterials);
}
//Cambiar de plantilla para materiales
function _changeTemplate(Object){
	if (Object.value=="0") return;
	var op = confirm("Esta seguro que desea cargar los valores de esta Plantilla?");
	if (op!=true) return;
	//var ideTemplate = Object.options[Object.selectedIndex].id;
	var ideTemplate = Object.value;
	while (Object.id!='divData'){Object = Object.parentNode;}
	
	var divData =  Object;
	var inputs = Object.getElementsByTagName("input");
	if (inputs.length>1) {	
		ide = 	inputs[0];
	}
	var divMaterials =   getElementById('divMaterialsDetails',Object, "div");
	
	while (Object.id!='divBody'){Object = Object.parentNode;}
	var table =   getElementById('dataTable',Object, "table");
	var page = table.getAttribute("page");
	var name = table.getAttribute("name");
	
	var url = _PATH+_SYSTEM+"?name="+name+"&type=loadTemplate&ide="+ide.value+"&ideT="+ideTemplate;
	divMaterials.innerHTML="Cargando...";
	//alert (url);
	getPageHTML(url, divMaterials);
}

/*
	@name = Pedidos
*/
//Mostrar los detalles del presupuesto
function _showDetailsPresupuesto(Object){
	while (Object.tagName!='DIV'){Object = Object.parentNode;}
	var divSearch =  Object;
	var inputs = Object.getElementsByTagName("input");
	if (inputs.length>1){
		inputs = inputs[0];
	}
	while (Object.id!='divData'){Object = Object.parentNode;}
	var divData =  Object;
	var divDetails =   getElementById('divDetails',Object, "div");
	
	while (Object.id!='divBody'){Object = Object.parentNode;}
	var table =   getElementById('dataTable',Object, "table");
	var page = table.getAttribute("page");
	var name = table.getAttribute("name");
	
	var url = _PATH+_SYSTEM+"?name="+name+"&type=loadDetails&ide="+inputs.value;
	divDetails.innerHTML="Cargando...";
	//alert (url);
	getPageHTML(url, divDetails);
}
//Para Guardar Presupuesto
function _save_Order(Object){
	while (Object.id!='divData'){Object = Object.parentNode;}
	var tableSave = Object.getElementsByTagName("table");
	if (tableSave.length>=1)	tableSave = tableSave[0];
	var error="";
	var rpta="";
	var inputs = Object.getElementsByTagName("input");
	
	for (i=0;i<inputs.length;i++){
		if (inputs[i].id=="") continue;
		//rpta+=inputs[i].id+"="+inputs[i].value+"&";
		var isnull=inputs[i].getAttribute("isnull");
		if (isnull=="false" && inputs[i].value=="") error="123";
		rpta+=inputs[i].id+"*"+inputs[i].value+"$";
	}
	
	while (Object.id!='divData'){Object = Object.parentNode;}
	var divData = Object;
	var divDetails =   getElementById('divDetails',Object, "div");
	var trs = divDetails.getElementsByTagName("tr");
	rpta+="&details=";
	for (i=0;i<trs.length;i++){
		if (trs[i].id=="") continue;
		var inputs = trs[i].getElementsByTagName("input");
		if (inputs.length==0 || !inputs[0].checked) continue;
		rpta+=trs[i].id+"*";
	}
	
	while (Object.id!='divBody'){Object = Object.parentNode;}
	
	var where = _get_Find(Object);
	var where0= _get_Ides(Object);
	var table =   getElementById('dataTable',Object, "table");
	var page = table.getAttribute("page");
	var name = table.getAttribute("name");
	
	//while (Object.id!='divShow'){Object = Object.parentNode;}
	divData.innerHTML="";
	var divData =  getElementById('divShow',Object,"div");
	var url = _PATH+_SYSTEM+"?values="+rpta+"&name="+name+"&type=saveItem&where="+where+"&where0="+where0+"&page="+page+"&ides="+where0;
	if (tableSave.id!="") url+="&ide="+tableSave.id;
	divData.innerHTML="Cargando...";
	//alert (url);
	getPageHTML(url, divData);
	
}
/*
@name = Pedidos Generales
*/
function _showDetailsOrder(Object){
	while (Object.tagName!='DIV'){Object = Object.parentNode;}
	var divSearch =  Object;
	var inputs = Object.getElementsByTagName("input");
	if (inputs.length>1){
		inputs = inputs[0];
	}
	while (Object.id!='divData'){Object = Object.parentNode;}
	var divData =  Object;
	var divDetails =   getElementById('divDetails',Object, "div");
	
	while (Object.id!='divBody'){Object = Object.parentNode;}
	var table =   getElementById('dataTable',Object, "table");
	var page = table.getAttribute("page");
	var name = table.getAttribute("name");
	
	var url = _PATH+_SYSTEM+"?name="+name+"&type=loadDetails&ide="+inputs.value;
	divDetails.innerHTML="Cargando...";
	//alert (url);
	getPageHTML(url, divDetails);
}

/*
@name = compras
*/
function _calculateTotal(Object){
	var monto = Object.value;
	while (Object.tagName!='TR'){Object = Object.parentNode;}
	var tds = Object.getElementsByTagName("td");
	if (tds.length<3)return;
	var can = tds[1].getElementsByTagName('input')[0].value;
	var total = round(monto * can,2);
	var original=parseFloat(total);
	var result=Math.round(original*100)/100 ;

	var inputs = tds[4].getElementsByTagName('input');
	tds[4].getElementsByTagName('input')[0].value = total;
}
function _calculateUni(Object){
	var monto = Object.value;
	while (Object.tagName!='TR'){Object = Object.parentNode;}
	var tds = Object.getElementsByTagName("td");
	if (tds.length<3)return;
	var can = tds[1].getElementsByTagName('input')[0].value;
	var total = round(monto / can,2);
	tds[3].getElementsByTagName('input')[0].value = total;
}

function _save_Buy(Object){
	while (Object.id!='divNew'){Object = Object.parentNode;}
	var inputs =  Object.getElementsByTagName("input");
	for (j=0;j<inputs.length;j++){
		if (inputs[j].value=="" || inputs[j].value=="0") {alert ("Falta ingresar valores");inputs[j].focus();return;}
	}
	
	var tableSave = Object.getElementsByTagName("table");
	if (tableSave.length>=1)	tableSave = tableSave[0];
	var error="";
	var rpta="";
	var inputs = Object.getElementsByTagName("input");
	
	for (i=0;i<inputs.length;i++){
		if (inputs[i].id=="") continue;
		//rpta+=inputs[i].id+"="+inputs[i].value+"&";
		var isnull=inputs[i].getAttribute("isnull");
		if (isnull=="false" && inputs[i].value=="") error="123";
		rpta+=inputs[i].id+"*"+inputs[i].value+"$";
	}
	
	while (Object.id!='divData'){Object = Object.parentNode;}
	var divData = Object;
	var divDetails =   getElementById('divDetails',Object, "div");
	var trs = divDetails.getElementsByTagName("tr");
	rpta+="&details=";
	for (i=0;i<trs.length;i++){
		var inputs =  trs[i].getElementsByTagName("input");
		if (inputs.length==0)continue;
		for (j=0;j<inputs.length;j++){
			rpta+=inputs[j].value+"*"
		}
		rpta+="/";
	}
	
	while (Object.id!='divBody'){Object = Object.parentNode;}
	
	var where = _get_Find(Object);
	var where0= _get_Ides(Object);
	var table =   getElementById('dataTable',Object, "table");
	var page = table.getAttribute("page");
	var name = table.getAttribute("name");
	
	//while (Object.id!='divShow'){Object = Object.parentNode;}
	divData.innerHTML="";
	var divData =  getElementById('divShow',Object,"div");
	var url = _PATH+_SYSTEM+"?values="+rpta+"&name="+name+"&type=saveItem&where="+where+"&where0="+where0+"&page="+page+"&ides="+where0;
	if (tableSave.id!="") url+="&ide="+tableSave.id;
	divData.innerHTML="Cargando...";
	//alert (url);
	getPageHTML(url, divData);
}
/* 
	@name = Despacho
*/

function _save_Despacho(Object){
	while (Object.id!='divNew'){Object = Object.parentNode;}
	var inputs =  Object.getElementsByTagName("input");
	var tableSave = Object.getElementsByTagName("table");
	if (tableSave.length>=1)	tableSave = tableSave[0];
	var error="";
	var rpta="";
	var inputs = Object.getElementsByTagName("input");
	
	for (i=0;i<inputs.length;i++){
		if (inputs[i].id=="") continue;
		//rpta+=inputs[i].id+"="+inputs[i].value+"&";
		var isnull=inputs[i].getAttribute("isnull");
		if (isnull=="false" && inputs[i].value=="") {
			alert ("Falta ingresar valores");inputs[j].focus();return;
			//error="123";
		}
		rpta+=inputs[i].id+"*"+inputs[i].value+"$";
	}
	if (error!="")
	while (Object.id!='divData'){Object = Object.parentNode;}
	var divData = Object;
	var divDetails =   getElementById('divDetails',Object, "div");
	var trs = divDetails.getElementsByTagName("tr");
	rpta+="&details=";
	for (i=0;i<trs.length;i++){
		var inputs =  trs[i].getElementsByTagName("input");
		if (inputs.length==0)continue;
		for (j=0;j<inputs.length;j++){
			rpta+=inputs[j].value+"*"
		}
		rpta+="/";
	}
	
	while (Object.id!='divBody'){Object = Object.parentNode;}
	
	var where = _get_Find(Object);
	var where0= _get_Ides(Object);
	var table =   getElementById('dataTable',Object, "table");
	var page = table.getAttribute("page");
	var name = table.getAttribute("name");
	
	//while (Object.id!='divShow'){Object = Object.parentNode;}
	divData.innerHTML="";
	var divData =  getElementById('divShow',Object,"div");
	var url = _PATH+_SYSTEM+"?values="+rpta+"&name="+name+"&type=saveItem&where="+where+"&where0="+where0+"&page="+page+"&ides="+where0;
	if (tableSave.id!="") url+="&ide="+tableSave.id;
	divData.innerHTML="Cargando...";
	//alert (url);
	getPageHTML(url, divData);
}

function _text_max(Object, maxValue){
	if (Object.value > maxValue) Object.value = maxValue;
}

function _setValue(Object, maxValue){
	Object.value = maxValue;
}

//Mostrar los detalles de Despacho y Devolucion
function _showDetails(Object){
	//Obtener Variables
	while (Object.tagName!='TR'){Object = Object.parentNode;}
	var id = Object.id;
	while (Object.id!='dataTable'){Object = Object.parentNode;}
	var name = Object.getAttribute('name');
	
	while (Object.id!='divBody'){Object = Object.parentNode;}
	var divData =  getElementById('divData',Object,"div");
	var url = _PATH+_SYSTEM+"?name="+name+"&type=showDetails&ide="+id;
	divData.innerHTML="Cargando...";
	getPageHTML(url, divData);
}
//Imprimir los detalles en despacho y devolucion
function printDivData(Object){
	while (Object.id!='divBody'){Object = Object.parentNode;}
	var divData =  getElementById('divData',Object);
	buttons = divData.getElementsByTagName('button');
	buttons[0].style.display = 'none';
	buttons[1].style.display = 'none';
	printDiv(divData);
	buttons[0].style.display = '';
	buttons[1].style.display = '';
}

/*
	@name = Reportes
*/
//Mostrar u Ocultar Opciones
function getContentReport(Object){
	var ide=Object.id;
	while (Object.tagName!='TD'){Object = Object.parentNode;}
	var divContent =  getElementById('divReportContent',Object,"div");
	var show = divContent.getAttribute('show');
	if (show==""){
		divContent.setAttribute('show','false');
		while (Object.id!='divData'){Object = Object.parentNode;}
		var divData= Object;
		var name = Object.getAttribute('name');
		var url = _PATH+_SYSTEM+"?name="+name+"&type=showOptions&idx="+ide;
		divContent.innerHTML="Cargando...";
		getPageHTML(url, divContent);
	}
	else if (show=="false"){
		divContent.style.display='none';
		divContent.setAttribute('show','true');
	}
	else{
		divContent.style.display='';
		divContent.setAttribute('show','false');
	}
	
	
}

//Mostrar el Reporte
function showReport(Object, idx){
	var ide=Object.id;
	while (Object.id!='divReportContent'){Object = Object.parentNode;}
	rpta="values=";
	var inputs = Object.getElementsByTagName("input");
	for (i=0;i<inputs.length;i++){
		if (inputs[i].id=="") continue;
		rpta+=inputs[i].id+"*"+inputs[i].value+"$";
	}
	var selects = Object.getElementsByTagName("select");
	for (i=0;i<selects.length;i++){
		if (selects[i].id=="") continue;
		rpta+=selects[i].id+"*"+selects[i].value+"$";
	}
	var divReport = Object;
	while (Object.id!='divData'){Object = Object.parentNode;}
	var divData = Object;
	var name = divData.getAttribute('name');
	var url = _PATH+_SYSTEM+"?name="+name+"&idx="+idx+"&type=showData&file="+ide+"&"+rpta;
	if (ide=='HTML'){
		var divReportShow =  getElementById('divShowReport',divReport,"div");
		//alert (divReportShow.innerHTML);
		divReportShow.innerHTML="Cargando...";
		getPageHTML(url, divReportShow);
	}
	else {
		var iframe =  getElementById('iframeShow',Object,"iframe");
		iframe.src=url;
	}
}
function _clear_divReport(Object){
	var ide=Object.id;
	while (Object.id!='divReportContent'){Object = Object.parentNode;}
	Object.setAttribute('show','true');
	Object.innerHTML="";
}